package manager

import (
	"BlazeRider-backend/app/models"
	"BlazeRider-backend/config"
	"fmt"
)

// UserInfo 存储用户信息的结构体
type UserInfo struct {
	UserID   string
	UserName string
	HeadImg  string
	Level    int
	Rank     int
	Score    int
}

type UserManager struct {
	storage UserStorage
}

type LiveAuthInfo struct {
	//"room_id": 7214015683695250235,
	//"anchor_open_id":" "_000oJIu6APhomK7KIBGqSYm5XYPxCJB_xxx",
	//"avatar_url": "https://p11.douyinpic.com/aweme/720x720/aweme-avatar/tos-cn-avt-0015_973c31e8055f78a41d3f7de3def9821d.jpeg?from=3067671334",
	//"nick_name": "xxx"
}

func NewUserManager() (*UserManager, error) {
	var storage UserStorage
	var err error

	if config.Get().Database.Enabled {
		storage, err = newUserMySQLStorage()
		if err != nil {
			return nil, fmt.Errorf("failed to init MySQL storage: %v", err)
		}
	} else {
		storage = newUserMemStorage()
	}

	return &UserManager{storage: storage}, nil
}

func (um *UserManager) InitializeUser(userID, userName, headImg string, level int) error {
	return um.storage.InitializeUser(userID, userName, headImg, level)
}

func (um *UserManager) GetUser(userID string) (*UserInfo, error) {
	return um.storage.GetUser(userID)
}

func (um *UserManager) RecordScore(userID string, score int) error {
	return um.storage.RecordScore(userID, score)
}

func (um *UserManager) SetUserScoreByLiveRoomResult() error {
	return um.storage.SetUserScoreByLiveRoomResult()
}

func (um *UserManager) SetUserLevelByLevelSet(levelSetArr map[int]LevelSet) error {
	return um.storage.SetUserLevelByLevelSet(levelSetArr)
}

//func (um *UserManager) SetAuth(liveRoomToken string) (*utils.LiveRoomUserInfo, string, error) {
//	return um.storage.SetAuth(liveRoomToken)
//}
//
//func (um *UserManager) CheckAuth(liveRoomLoginToken string) (*utils.MyClaims, error) {
//	return um.storage.CheckAuth(liveRoomLoginToken)
//}

func (um *UserManager) GetRanks() (*models.RankInfos, error) {
	return um.storage.GetRanks()
}

func (um *UserManager) GetWorldRecords() (*models.WorldRecods, error) {
	return um.storage.GetWorldRecords()
}
